package com.liusp.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.liusp.dao.OrderSettingDao;
import com.liusp.pojo.OrderSetting;
import com.liusp.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        //如果该日期下没有设置可预约数，则新增
        OrderSetting os = orderSettingDao.seleteOrderSetting(orderSetting.getOrderDate());
        if (os == null){
            //新增
            OrderSetting setting = new OrderSetting();
            setting.setNumber(orderSetting.getNumber());
            setting.setOrderDate(orderSetting.getOrderDate());
            setting.setReservations(0);
            orderSettingDao.insertOrderSetting(setting);
        }else {
            //修改
            //设置预约数不能小于当前已预约数
            int reservations = orderSettingDao.seleteReservationsByOrderDate(orderSetting);
            if (orderSetting.getNumber() < reservations){
                throw new RuntimeException("修改预约数不能小于当前已预约人数");
            }
            orderSettingDao.editNumberByDate(orderSetting);
        }
    }

    /**
     * 使用map封装返回结果，前端不需要修改
     * @param date 年-月（2025-09）
     * @return 结果集合
     */
    @Override
    public List<Map<String,Object>> getOrderSettingByMonth(String date) {
        // 将传入的日期格式化为标准格式，如将"2025-9"转换为"2025-09"
        String formattedDate = formatDateToStandard(date);
        List<OrderSetting> orderSettingByMonth = orderSettingDao.getOrderSettingByMonth(formattedDate);
        //直接使用map封装返回
        List<Map<String,Object>> result = new ArrayList<>();
        orderSettingByMonth.forEach(orderSetting -> {
            Map<String,Object> map = new HashMap<>();
            int i = orderSetting.getOrderDate().getDate();
            map.put("id",orderSetting.getId());
            map.put("orderDate",orderSetting.getOrderDate());
            map.put("number",orderSetting.getNumber() - orderSetting.getReservations());
            map.put("reservations",orderSetting.getReservations());
            map.put("date",i);
            result.add(map);
        });
        return result;
    }

    /**
     * 直接返回OrderSetting的集合，前端需要修改
     * @param date 年-月（2025-09）
     * @return 结果集合
     */
    /*@Override
    public List<OrderSetting> getOrderSettingByMonth(String date) {
        // 将传入的日期格式化为标准格式，如将"2025-9"转换为"2025-09"
        String formattedDate = formatDateToStandard(date);
        return orderSettingDao.getOrderSettingByMonth(formattedDate);
    }*/

    /**
     * 转换传入参数格式为 yyyy-MM
     * @param date 日期(yyyy-M)
     * @return yyyy-MM字符串
     */
    private String formatDateToStandard(String date) {
        if (date != null && date.contains("-")) {
            String[] parts = date.split("-");
            if (parts.length == 2) {
                String year = parts[0];
                String month = parts[1];
                // 确保月份为两位数格式
                if (month.length() == 1) {
                    month = "0" + month;
                }
                return year + "-" + month;
            }
        }
        return date;
    }

}
